package dk.twu.analyzer;

import dk.twu.analyzer.core.IntraDayAnalyzer;
import dk.twu.analyzer.model.IntraDayInput;
import dk.twu.analyzer.model.MatchEntry;
import dk.twu.analyzer.util.AnalyzerConfig;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collection;

public class AppAnalyze {
    private static Logger logger = LoggerFactory.getLogger(AppAnalyze.class);
    private static final String C_BASE_DIR = "D:\\Workspace\\EOD\\";
    private static final String C_OUTPUT_DIR = "D:\\Workspace\\output\\";
    private static final String C_EXCHANGE_LIST_XML = "ExchangeList.xml";
    private static final String C_SYMBOL_LIST_XML = "SymbolList.xml";
    public static final AnalyzerConfig ANALYZER_CONFIG = new AnalyzerConfig(C_BASE_DIR, C_OUTPUT_DIR, C_EXCHANGE_LIST_XML,
            C_SYMBOL_LIST_XML);

    public static void main(String[] args) {
        IntraDayInput input = new IntraDayInput(15, LocalDate.of(2015, 5, 18), 1, LocalTime.of(9, 15), LocalTime.of
                (16, 15), Arrays.asList("NYSE", "NASDAQ"), Arrays.asList("MSFT.NASDAQ", "AAPL.NASDAQ", "JPM.NYSE",
                "RCPI" + ".NASDAQ", "AAL" + ".NASDAQ"));
        IntraDayAnalyzer itdAnalyzer = new IntraDayAnalyzer(ANALYZER_CONFIG);
        Collection<MatchEntry> matchEntries = itdAnalyzer.getDayMatchEntry(input);
        exportMatchEntry("COMPLETE", matchEntries);
    }

    private static void exportMatchEntry(String tag, Collection<MatchEntry> matchEntries) {
        Path p = Paths.get(ANALYZER_CONFIG.getOutputDir() + "match_entry_" + tag + ".csv");
        try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(p, StandardOpenOption.CREATE,
                StandardOpenOption.APPEND))) {
            for (MatchEntry e : matchEntries) {
                byte queryBytes[] = e.toCSVLine().getBytes();
                out.write(queryBytes, 0, queryBytes.length);
            }
            out.close();
        } catch (IOException x) {
            logger.error(x.toString());
        }
    }

}
